package com.example.demo.service.impl;

import com.example.demo.bean.PageBean;
import com.example.demo.bean.product;
import com.example.demo.dao.FirstDao;
import com.example.demo.service.IFirstService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FirstServiceImpl implements IFirstService {
    @Autowired
    FirstDao firstDao;

    @Override
    public int getCount() {
        return firstDao.getAll();
    }

    @Override
    public void save(product p) {
        firstDao.save(p);
    }

    @Override
    public PageBean find(int currentPage, int MaxSize) {
        PageBean pageBean = new PageBean();
        int totalRecord = firstDao.getAll();
        int startIndex = (currentPage - 1) * MaxSize;
        List<product> items = firstDao.getItems(startIndex, MaxSize);
        int pageSize = MaxSize;
        int totalPage = totalRecord / MaxSize + 1;
        pageBean.setCurrentPage(currentPage);
        pageBean.setTotalRecord(totalRecord);
        pageBean.setPageSize(pageSize);
        pageBean.setItems(items);
        pageBean.setTotalPage(totalPage);
        return pageBean;
    }

    @Override
    public product findone() {
        return firstDao.findone();
    }
}
